/**
 * 	 Copyright (c) 2008, 2009 Lukas Zaruba
 * 
 *   This file is part of UniAnalyzer (Universal Analyzer).
 *
 *   UniAnalyzer is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   UniAnalyzer is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with UniAnalyzer. If not, see <http://www.gnu.org/licenses/>.
 */

package net.zarubsys.unianalyzer;

import java.net.URL;

import net.zarubsys.unianalyzer.plugin.UnianalyzerPlugin;
import net.zarubsys.unianalyzer.preferences.PreferenceConstants;

import org.apache.log4j.Logger;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialogWithToggle;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.application.IWorkbenchConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchAdvisor;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.internal.ide.IDEInternalWorkbenchImages;
import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
import org.eclipse.ui.internal.ide.model.WorkbenchAdapterBuilder;
import org.osgi.framework.Bundle;

@SuppressWarnings("restriction")
public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {

	private static final String PERSPECTIVE_ID = "net.zarubsys.unianalyzer.perspective"; //$NON-NLS-1$
	
	private static final Logger unhandledLoopLog = Logger.getLogger("UnhandledLoopLogger");
	
	@Override
	public void initialize(IWorkbenchConfigurer configurer) {
		configurer.setSaveAndRestore(true);
		super.initialize(configurer);
		WorkbenchAdapterBuilder.registerAdapters();
		 
		final String ICONS_PATH = "icons/full/"; //$NON-NLS-1$
		final String PATH_OBJECT = ICONS_PATH + "obj16/"; //$NON-NLS-1$
		final String PATH_WIZBAN = ICONS_PATH + "wizban/"; //$NON-NLS-1$
		Bundle ideBundle = Platform.getBundle(IDEWorkbenchPlugin.IDE_WORKBENCH);
		declareWorkbenchImage(configurer, ideBundle,
				IDE.SharedImages.IMG_OBJ_PROJECT, PATH_OBJECT + "prj_obj.gif", //$NON-NLS-1$
				true);
		declareWorkbenchImage(configurer, ideBundle,
				IDE.SharedImages.IMG_OBJ_PROJECT_CLOSED, PATH_OBJECT
						+ "cprj_obj.gif", true); //$NON-NLS-1$
		declareWorkbenchImage(configurer, ideBundle,
			    IDEInternalWorkbenchImages.IMG_DLGBAN_SAVEAS_DLG, PATH_WIZBAN
			      + "saveas_wiz.png", true); //$NON-NLS-1$
	}
 
	private void declareWorkbenchImage(IWorkbenchConfigurer configurer_p,
			Bundle ideBundle, String symbolicName, String path, boolean shared) {
		URL url = ideBundle.getEntry(path);
		ImageDescriptor desc = ImageDescriptor.createFromURL(url);
		configurer_p.declareImage(symbolicName, desc, shared);

	}

    public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
        return new ApplicationWorkbenchWindowAdvisor(configurer);
    }

	public String getInitialWindowPerspectiveId() {
		return PERSPECTIVE_ID;
	}
	
	@Override
	public boolean preShutdown() {
		IPreferenceStore prefStore = UnianalyzerPlugin.getDefault().getPreferenceStore();
		boolean promptOnExit = prefStore.getBoolean(PreferenceConstants.PROMPT_ON_EXIT);
		boolean close = true;
		if (promptOnExit) {
			MessageDialogWithToggle dlg = MessageDialogWithToggle
             .openOkCancelConfirm(getWorkbenchConfigurer().getWorkbench().getActiveWorkbenchWindow()
                        .getShell(), Messages.getString("ApplicationWorkbenchAdvisor.0"), Messages.getString("ApplicationWorkbenchAdvisor.1") //$NON-NLS-1$ //$NON-NLS-2$
                        + Platform.getProduct().getName() + Messages.getString("ApplicationWorkbenchAdvisor.2"), //$NON-NLS-1$
                        Messages.getString("ApplicationWorkbenchAdvisor.3"), false, null, null); //$NON-NLS-1$
			close = dlg.getReturnCode() == IDialogConstants.OK_ID;
			prefStore.setValue(PreferenceConstants.PROMPT_ON_EXIT, !dlg.getToggleState());
		}
		return close;
	}	
	
	public IAdaptable getDefaultPageInput() {
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		return workspace.getRoot();
	}
	
	@Override
	public void eventLoopException(Throwable exception) {
		unhandledLoopLog.error("Unhandled loop error occured", exception);
	}

}
